// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import { time } from "console";
import * as vscode from "vscode";
import { toast, setStatusBarMessage, showInformationMessage } from "./utils";

// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
  // Use the console to output diagnostic information (console.log) and errors (console.error)
  // This line of code will only be executed once when your extension is activated
  console.log(
    'Congratulations, your extension "countdown-for-work" is now active!'
  );

  // The command has been defined in the package.json file
  // Now provide the implementation of the command with registerCommand
  // The commandId parameter must match the command field in package.json
  let targetTimeInput: string = "";
  let timer:any = null;
  // 弹窗提示
  function displayInfo() {
    const targetTime = new Date(targetTimeInput).getTime();
    if (!targetTime) {
      return;
    }
    const nowTime = new Date().getTime();
    if (targetTime <= nowTime) {
      setStatusBarMessage("你可以下班了，滚 😡 ！！！");
      return;
    }
    const del = (targetTime - nowTime) / 1000;
    const h = Math.floor((del / 60 / 60) % 60);
    const m = Math.floor((del / 60) % 60);
    const s = Math.floor(del % 60);

    const delstr = `${h}小时${m}分${s}秒`;
    // Display a message box to the user
    setStatusBarMessage(`你才刚刚上班！还有${delstr}才下班，等死吧！！！`);
    timer = setTimeout(displayInfo, 1000);
  }
  // 激活插件
  let init = vscode.commands.registerCommand("CFW.start", ()=>{
    targetTimeInput = (timer && targetTimeInput) || `${new Date().toLocaleDateString()} 21:00:00`;
    console.log(targetTimeInput);
    
    displayInfo();
  });
  // 设置下班时间
  let setRemainTime = vscode.commands.registerCommand("CFW.set", async () => {
    targetTimeInput = (await vscode.window.showInputBox()) || "";
    if (timer) {
      clearTimeout(timer);
      timer = null;
    }
    displayInfo();
  });

  // 注入注册后的命令
  context.subscriptions.push(init, setRemainTime);
}

// this method is called when your extension is deactivated
export function deactivate() {}
